package cn.getech.data.development.controller.real;

import cn.getech.data.development.service.RealTimeTaskOpsService;
import cn.getech.data.intelligence.common.utils.BaseRes;
import cn.getech.data.intelligence.common.utils.PageUtils;
import cn.getech.data.intelligence.common.utils.R;
import cn.getech.system.center.annotation.SysLog;
import cn.getech.system.center.constant.OperationModulesEnum;
import cn.getech.system.center.constant.OperationTypeEnum;
import cn.getech.system.center.constant.module.ModuleEnum;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(tags = "实时任务")
@RestController
@RequestMapping("/bdp/realTime/task/history")
public class RealTimeTaskHistoryController {

    @Autowired
    private RealTimeTaskOpsService realTimeTaskOpsService;


    @ApiOperation("历史运行")
    @PostMapping("/list")   // /data-development/bdp/realTime/task/history/list
    public R list(@RequestBody RealSearchReq param) {
        PageUtils<RealTimeTaskHistory> page = realTimeTaskOpsService.findRunHistoryList(param);
        return R.okWithData(page);
    }
    @ApiOperation("获取项目查询条件")
    @PostMapping("/condition")   // /data-development/bdp/realTime/task/history/condition
    public R projectList() {
        JSONObject data = realTimeTaskOpsService.findConditionList();
        return R.okWithData(data);
    }
    @ApiOperation("下载")
    @PostMapping("/download") // /data-development/bdp/realTime/task/history/download
    @SysLog(value="下载任务运维",type= OperationTypeEnum.DOWNLOADCODE,
            modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.OPERATION_CENTER_MODULE_CODE)

    public R download(HttpServletResponse response,@RequestBody RealSearchReq param) {
        String fileName = "模板文件" + System.currentTimeMillis();

        response.setHeader("Content-Type", "application/octet-stream");
        ExcelWriter excelWriter = null;
        try {
            // 清空response
            response.reset();
            fileName = String.format("%s.xlsx", URLEncoder.encode(fileName, "utf-8"));
            response.setHeader("filename", fileName);
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/octet-stream");
            ServletOutputStream os = response.getOutputStream();
            param.setPage(1);
            param.setLimit(99999);
            PageUtils<RealTimeTaskHistory> page = realTimeTaskOpsService.findRunHistoryList(param);
            List<RealTimeTaskHistoryExcel> historyExcels = Lists.newArrayList();
            for (RealTimeTaskHistory realTimeTaskHistory : page.getList()) {
                historyExcels.add(new RealTimeTaskHistoryExcel(realTimeTaskHistory));
            }
            excelWriter = EasyExcel.write(os,
                    RealTimeTaskHistoryExcel.class)
                    .build();
            WriteSheet sheet = EasyExcel.writerSheet().build();
            excelWriter.write(historyExcels, sheet).finish();
        } catch (IOException e) {
            return R.error("模板文件错误");
        } finally {
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
        return null;
    }
}
